#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   App_RoutineControl_0x31_test.py
@Time    :   2022/12/15 13:31:28
@Author  :   YangLiang 
@Version :   1.0
@Contact :   YangLiang@chengtech.com
@License :   (C)Copyright 2022-2025
@Desc    :   None
'''

# here put the import lib
from pytest_assume.plugin import assume

import pytest
import time
from sympy import I
# from sympy import assuming
from commDriver.TS_CAN import *
import allure
import sys
from collections.abc import Iterable

from sqlalchemy import false
sys.dont_write_bytecode = True

RoutinControl = [   ([0x31, 0x01, 0x02, 0x03], [0x71, 0x01, 0x02, 0x03]),
                    ([0x31, 0x01, 0x12, 0x7A], [0x71, 0x01, 0x12, 0x7A, 0x01]),
                    ([0x31, 0x01, 0x12, 0x7B], [0x71, 0x01, 0x12, 0x7B, 0x01]),
                    ([0x31, 0x03, 0x12, 0x7A], [0x71, 0x03, 0x12, 0x7A]),
                    ([0x31, 0x02, 0x12, 0x7A], [0x71, 0x02, 0x12, 0x7A]),
                    ([0x31, 0x03, 0x12, 0x7B], [0x71, 0x03, 0x12, 0x7B]),
                    ([0x31, 0x02, 0x12, 0x7B], [0x71, 0x02, 0x12, 0x7B])
                ]

RoutinControl_01 = [([0x31, 0x01, 0x02, 0x03], [0x71, 0x01, 0x02, 0x03]),
                    ([0x31, 0x01, 0x12, 0x7A], [0x71, 0x01, 0x12, 0x7A, 0x01]),
                    ([0x31, 0x01, 0x12, 0x7B], [0x71, 0x01, 0x12, 0x7B, 0x01])]

NRC_13_33_31 = [([0x31], [0x7F, 0x31, 0x13]),  # NRC13
                ([0x31, 0x01], [0x7F, 0x31, 0x13]),
                ([0x31, 0x02], [0x7F, 0x31, 0x13]),
                ([0x31, 0x03], [0x7F, 0x31, 0x13]),
                ([0x31, 0x01, 0x02], [0x7F, 0x31, 0x13]),
                ([0x31, 0x02, 0x12], [0x7F, 0x31, 0x13]),
                ([0x31, 0x03, 0x12], [0x7F, 0x31, 0x13]),
                ([0x31, 0x01, 0x02, 0x03], [0x71, 0x01, 0x02, 0x03]),
                ([0x31, 0x01, 0x02, 0x03, 0x00], [0x7F, 0x31, 0x13]),
                ([0x31, 0x01, 0x12, 0x7A], [0x7F, 0x31, 0x33]),
                ([0x31, 0x02, 0x12, 0x7A], [0x7F, 0x31, 0x33]),
                ([0x31, 0x03, 0x12, 0x7A], [0x7F, 0x31, 0x33]),
                ([0x31, 0x01, 0x12, 0x7B], [0x7F, 0x31, 0x33]),
                ([0x31, 0x02, 0x12, 0x7B], [0x7F, 0x31, 0x33]),
                ([0x31, 0x03, 0x12, 0x7B], [0x7F, 0x31, 0x33]),
                ([0x31, 0x01, 0x01, 0x22], [0x7F, 0x31, 0x31]),  # NRC31
                ([0x31, 0x02, 0x01, 0x22], [0x7F, 0x31, 0x31]),
                ([0x31, 0x03, 0x01, 0x22], [0x7F, 0x31, 0x31]),
                ([0x31, 0x01, 0x02, 0x02], [0x7F, 0x31, 0x31]),
                ([0x31, 0x01, 0xFF, 0x01], [0x7F, 0x31, 0x31]),
                ([0x31, 0x01, 0xFF, 0x00], [0x7F, 0x31, 0x31]),
                ([0x31, 0x03, 0x12, 0x70], [0x7F, 0x31, 0x31]),
                ([0x31, 0x01, 0x12, 0x0B], [0x7F, 0x31, 0x31]),
                ([0x31, 0x02, 0x12, 0x11], [0x7F, 0x31, 0x31]),
                ([0x31, 0x01, 0xFF, 0x01], [0x7F, 0x31, 0x31]),
                ([0x31, 0x02, 0x12, 0x11], [0x7F, 0x31, 0x31]),
                ([0x31, 0x01, 0x12, 0x11, 0x00], [0x7F, 0x31, 0x31]),
                ]

NRC_12_24 = [   ([0x31, 0x81, 0x02, 0x03], [0x7F, 0x31, 0x12]),
                ([0x31, 0x81, 0x12, 0x7A], [0x7F, 0x31, 0x12]),
                ([0x31, 0x81, 0x12, 0x7B], [0x7F, 0x31, 0x12]),
                ([0x31, 0x83, 0x12, 0x7A], [0x7F, 0x31, 0x12]),
                ([0x31, 0x82, 0x12, 0x7A], [0x7F, 0x31, 0x12]),
                ([0x31, 0x83, 0x12, 0x7B], [0x7F, 0x31, 0x12]),
                ([0x31, 0x82, 0x12, 0x7B], [0x7F, 0x31, 0x12]),
                ([0x31, 0x00, 0x12, 0x7A], [0x7F, 0x31, 0x12]),  # NRC12
                ([0x31, 0x04, 0x12, 0x7A], [0x7F, 0x31, 0x12]),
                ([0x31, 0x7F, 0x12, 0x7A], [0x7F, 0x31, 0x12]),
                ([0x31, 0x00, 0x12, 0x7B], [0x7F, 0x31, 0x12]),
                ([0x31, 0x04, 0x12, 0x7B], [0x7F, 0x31, 0x12]),
                ([0x31, 0x7F, 0x12, 0x7B], [0x7F, 0x31, 0x12]),
                ([0x31, 0x7E, 0x12, 0x7B], [0x7F, 0x31, 0x12]),
                ([0x31, 0x01, 0x12, 0x7A, 0x00], [0x7F, 0x31, 0x13]),
                ([0x31, 0x02, 0x12, 0x7A, 0x03], [0x7F, 0x31, 0x13]),
                ([0x31, 0x03, 0x12, 0x7A, 0x01], [0x7F, 0x31, 0x13]),
                ([0x31, 0x01, 0x12, 0x7B, 0x00], [0x7F, 0x31, 0x13]),
                ([0x31, 0x02, 0x12, 0x7B, 0x02], [0x7F, 0x31, 0x13]),
                ([0x31, 0x03, 0x12, 0x03, 0x03], [0x7F, 0x31, 0x31]),
                ([0x31, 0x01, 0x02, 0x03, 0x00], [0x7F, 0x31, 0x13]),
                ([0x31, 0x03, 0x12, 0x7B], [0x7F, 0x31, 0x24]),
                ([0x31, 0x01, 0x02], [0x7F, 0x31, 0x13]),
                ([0x31, 0x02, 0x01, 0x22], [0x7F, 0x31, 0x31]),
                ([0x31, 0x03, 0x01, 0x22], [0x7F, 0x31, 0x31]),
                ([0x31, 0x01, 0x02, 0x02], [0x7F, 0x31, 0x31]),
                ([0x31, 0x01, 0xFF, 0x01], [0x7F, 0x31, 0x31]),
                ([0x31, 0x01, 0xFF, 0x00], [0x7F, 0x31, 0x31]),
                ([0x31, 0x03, 0x12, 0x70], [0x7F, 0x31, 0x31]),
                ([0x31, 0x01, 0x12, 0x0B], [0x7F, 0x31, 0x31]),
                ([0x31, 0x02, 0x12, 0x11], [0x7F, 0x31, 0x31]),
                ([0x31, 0x01, 0xFF, 0x01], [0x7F, 0x31, 0x31]),
                ([0x31, 0x02, 0x12, 0x11], [0x7F, 0x31, 0x31]), 
            ]



class Test_RoutineControl_Phy(object):

    ids = ['Request {0}'.format(binascii.b2a_hex(bytearray(req), ' ')) for req, resp in NRC_12_24]
    @allure.severity('minor')
    @allure.description('Test NRC24 NRC12 of 0x31 in extended session with physical addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/6306e0482ce6d84a055b6e90/cases')
    @pytest.mark.parametrize('req,resp', NRC_12_24, ids=ids)
    def test_A_RoutineControl_NRC_12_24_Phy(self, action, Driver, req, resp):
        iStep = 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3, iStep, True)

    ids = ["Request {0}".format(binascii.b2a_hex(bytearray(req), ' ')) for req, resp in RoutinControl]

    @pytest.mark.smoke
    @allure.severity('normal')
    @allure.story('Routine conteol test')
    @allure.description('Test positive response of 0x31 RID in extended session with physical addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/6306e0482ce6d84a055b6e90/cases')
    @pytest.mark.parametrize('req,resp', RoutinControl, ids=ids)
    def test_B_RoutineControl_DID_Phy(self, action, Driver, req, resp):
        iStep = 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x31, 0x01] + req[2:], [0x71, 0x01] + req[2:4], 3, iStep, True)
        time.sleep(0.5)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 0, iStep, False)
        

    ids = ['Request {0}'.format(binascii.b2a_hex(bytearray(req), ' ')) for req, resp in RoutinControl]
    @allure.severity('minor')
    @allure.description('Test NRC7F of 0x31 in extended session with physical addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/6306e0482ce6d84a055b6e90/cases')
    @pytest.mark.parametrize('req,resp', RoutinControl, ids=ids)
    def test_RoutineControl_NRC7F_Phy(self, action, Driver, req, resp):
        iStep = 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, [0x7F, 0x31, 0x7F], 1, iStep, False)
        

    ids = ['Request {0}'.format(binascii.b2a_hex(bytearray(req), ' ')) for req, resp in NRC_13_33_31]
    @allure.severity('minor')
    @allure.description('Test NRC33 NRC31 NRC13 of 0x31 in extended session with physical addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/6306e0482ce6d84a055b6e90/cases')
    @pytest.mark.parametrize('req,resp', NRC_13_33_31, ids=ids)
    def test_RoutineControl_NRC_13_33_31_Phy(self, action, Driver, req, resp):
        iStep = 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3, iStep, False)